/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schtohan.restclient;

import java.io.StringReader;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import schtohan.business.Book;
import schtohan.business.Departure;

/**
 *
 * @author Schtohan
 */
@ManagedBean
@RequestScoped
public class NewJSFManagedBean {

    /**
     * Creates a new instance of NewJSFManagedBean
     */
    DocumentBuilderFactory dfac;
    DocumentBuilder dBuild;
    Document doc;
    
    
    public NewJSFManagedBean() throws ParserConfigurationException {
        dfac = DocumentBuilderFactory.newInstance();
        dBuild = dfac.newDocumentBuilder();   
        departures = new ArrayList();
    }
        
    private String bajskorv;
    private ArrayList<Departure> departures;

    public ArrayList<Departure> getDepartures() {
        return departures;
    }

    public void setDepartures(ArrayList<Departure> departures) {
        this.departures = departures;
    }

    public String getBajskorv() {
        return bajskorv;
    }

    public void setBajskorv(String bajskorv) {
        this.bajskorv = bajskorv;
    }
    
    public String theMethod()
    {
//        try
//        {
//            VasttrafikClient client = new VasttrafikClient();
//            bajskorv = client.departureBoard("9021014003760000");
//            InputSource inSource = new InputSource(new StringReader(bajskorv));
//            Document doc = dBuild.parse(inSource);
//            doc.getDocumentElement().normalize();
//            NodeList depList = doc.getElementsByTagName("Departure");
//
//            for(int i = 0; i < depList.getLength(); i++)
//            {
//                Node n = depList.item(i);
//                if (n.getNodeType() == Node.ELEMENT_NODE) {
//                    Departure d = new Departure();
//                    d.setName(((Element)n).getAttribute("name"));
//                    d.setTime(((Element)n).getAttribute("time"));
//                    d.setDirection(((Element)n).getAttribute("direction"));
//                    
//                    departures.add(d);
//                }
//            }     
//            
//        
//        }
//               
//        catch(Exception ex) {}
        NewJerseyClient client = new NewJerseyClient();
        Book b = new Book();
        b.setAuthor("Haruki Murakami");
        b.setRelYear("2009");
        b.setTitle("1Q84");
        client.create_XML(b);
                  
        return "index";
    }   
    
    public String getBook()
    {
//        NewJerseyClient client = new NewJerseyClient();
//        Book b = client.
//        b.setAuthor("Haruki Murakami");
//        b.setRelYear("2009");
//        b.setTitle("1Q84");
//        client.create_XML(b);
//                  
        return "index";
    } 
    
}
